<template>
	<view class="content">
		<navBar>
			<template @click="backPage()" slot="left">
				<u-icon name="arrow-left" color="#fff"></u-icon>
			</template>
			<template class="centerText" slot="center">
				<view class="" style="color: #fff">分类管理</view>
			</template>
		</navBar>
		<view class="popup_content">
			<view class="top_box">
				<view @click="addAndEditBtn()" class="btn">
					新增
				</view>
				<view class="ipt_box">
					<u-icon name="search" color="#c0c0c0" size="28"></u-icon>
					<input v-model="pageInfo.cate_name" @input="searchCategory()" placeholder="关键字查找类别" type="text" />
				</view>

				<!-- <view @click="close()" class="text_box">
					关闭
				</view> -->
			</view>
			<view v-if="categoryList.length == 0" class="empty_box">
				<u-empty text="暂无分类"></u-empty>
			</view>
			<!-- 分类列表-->
			<view v-else class="stashList_box">
				<view @click.stop="addAndEditBtn(item)" class="stashItem_box" v-for="(item,index) in categoryList"
					:key="item.cate_id">
					<view class="left">
						<u-icon name="grid" color="#1296db" size="40"></u-icon>
						<text>{{item.cate_name}}</text>
					</view>
					<view style="display: flex;">
						<view style="margin-right: 20rpx;" @click.stop="deleteCategory(item.cate_id)" class="right">
							删除
						</view>
						<view @click.stop="addAndEditBtn(item)" class="right">
							编辑
						</view>
					</view>
				</view>
			</view>
		</view>
	</view>
</template>

<script>
	import navBar from '@/components/navBar.vue'
	import {
		getCategoryListApi,
		deleteCategoryApi
	} from '@/api/categoryManage.js'
	export default {
		data() {
			return {
				categoryList: [],
				pageInfo: {
					page: 1,
					pageSize: 100,
					cate_name: ""
				},
				timer: null
			};
		},
		components: {
			navBar
		},
		onShow() {
			this.getCategoryList()
		},
		methods: {
			backPage() {
				uni.navigateBack();

			},
			//搜索类别
			searchCategory() {
				clearTimeout(this.timer)
				this.timer = setTimeout(() => {
					this.getCategoryList(this.pageInfo)
				}, 500)
			},
			//新增类别
			addAndEditBtn(item) {
				if (item) {
					delete item.create_by
					delete item.create_time
					delete item.org_id
					delete item.update_by
					delete item.update_time
					const res = JSON.stringify(item)
					uni.navigateTo({
						url: `/commonToolPages/newCategory/newCategory?brandInfo=${res}`
					})
				} else {
					uni.navigateTo({
						url: `/commonToolPages/newCategory/newCategory`
					})
				}
			},
			//删除类别
			async deleteCategory(cate_id) {

				const that = this
				uni.showModal({
					content: '确定要删除此分类吗',
					success: async function(res) {
						if (res.confirm) {
							await deleteCategoryApi({
								cate_id: cate_id
							})
							that.getCategoryList()

						} else if (res.cancel) {
							console.log('用户点击取消');
						}
					}
				});
			},
			//获取分类列表
			async getCategoryList() {
				const {
					data: {
						data: {
							list
						}
					}
				} = await getCategoryListApi(this.pageInfo)
				this.categoryList = list
			}
		}
	}
</script>

<style lang="scss">
	.content {
		height: 100vh;
		background-color: #f3f4f6;
	}

	.popup_content {
		background-color: #fff;
		margin: 20rpx 10rpx;
		border-radius: 20rpx;

		.top_box {
			display: flex;

			font-size: 26rpx;
			padding: 20rpx;

			.btn {
				width: 120rpx;
				height: 70rpx;
				background-color: #39cccc;
				color: #fff;
				text-align: center;
				line-height: 70rpx;
				border-radius: 35rpx;
				margin-right: 40rpx;
			}

			.ipt_box {
				display: flex;
				align-items: center;
				width: 480rpx;
				height: 70rpx;
				padding: 0 20rpx;
				background-color: #f2f2f2;
				border-radius: 35rpx;
				box-sizing: border-box;

				input {
					margin-left: 20rpx;
				}
			}

			.text_box {
				color: #60aeff;
				line-height: 70rpx;
			}
		}

		.stashList_box {
			height: 1300rpx;
			overflow: auto;
		}

		.stashItem_box {
			border-bottom: 1px solid #f2f2f2;
			display: flex;
			justify-content: space-between;
			align-items: center;
			padding: 20rpx;

			.left {
				display: flex;
				align-items: center;
				font-weight: 700;

				text {
					margin-left: 20rpx;
				}
			}

			.right {
				width: 90rpx;
				height: 50rpx;
				background-color: #39cccc;
				color: #fff;
				text-align: center;
				line-height: 50rpx;
				border-radius: 35rpx;
				font-size: 24rpx;
			}
		}
	}

	.empty_box {
		padding-top: 200rpx;
		height: 1000rpx;
		box-sizing: border-box;
	}
</style>